// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.IdPo;
import com.fowo.api.common.model.OptionItem;
import com.fowo.api.common.model.R;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.entity.FundsPayOrder;
import com.fowo.api.model.funds.pay.order.FundsPayOrderItemVo;
import com.fowo.api.model.funds.pay.order.FundsPayOrderSearchParamPo;
import com.fowo.api.model.funds.pay.order.FundsPayOrderVo;
import com.fowo.api.service.FundsPayOrderService;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "请款单")
@RequestMapping("/api/funds-pay-order")
@RestController
public class FundsPayOrderController {

  @Resource
  private FundsPayOrderService service;

  @Operation(summary = "查询结果")
  @GetMapping
  public R<List<FundsPayOrderItemVo>> search(
    FundsPayOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.search(search));
  }

  @Operation(summary = "快速查询选项(有缓存)")
  @GetMapping("options")
  public R<List<OptionItem<FundsPayOrderItemVo>>> searchOptions(
    FundsPayOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchOptions(search));
  }

  @Operation(summary = "查询最后更新时间")
  @GetMapping("lastUpdateTime")
  public R<Date> searchLastUpdateTime(FundsPayOrderSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchLastUpdateTime(search));
  }

  @Operation(summary = "获取分页查询结果")
  @GetMapping("page")
  public R<Page<FundsPayOrderItemVo>> getPage(
    FundsPayOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageSearch(search));
  }

  @Operation(summary = "查询是否后台下载")
  @GetMapping("getBackgroundExportFlag")
  public R<Boolean> getBackgroundExportFlag(FundsPayOrderSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.getBackgroundExportFlag(search));
  }

  @Operation(summary = "获取详情")
  @GetMapping("{id}")
  public R<FundsPayOrderVo> getVoById(@PathVariable Long id) {
    return R.data(() -> service.getVoById(id));
  }

  @Operation(summary = "获取关联单号相关的编号列表")
  @GetMapping("associated-order/{texts}/ids")
  public R<List<Long>> getIdsByAssociatedOrder(@PathVariable String texts) {
    List<String> nameList = Arrays
      .stream(texts.split(","))
      .collect(Collectors.toList());
    return R.data(() -> service.getIdsByAssociatedOrder(nameList));
  }

  @Operation(summary = "获取编号相关的关联单号列表")
  @GetMapping("associated-order/{ids}")
  public R<List<String>> getVoById(@PathVariable String ids) {
    List<Long> idList = Arrays
      .asList(ids.split(","))
      .stream()
      .map(Long::valueOf)
      .collect(Collectors.toList());
    return R.data(() -> service.getAssociatedOrderByIds(idList));
  }

  @PreAuthorize("@am.canAccess('fundsPayOrderCreate')")
  @Operation(summary = "新增")
  @PostMapping
  public R<Long> create(@Validated @RequestBody FundsPayOrderVo model) {
    return R.data(() -> service.create(model));
  }

  @PreAuthorize("@am.canAccess('fundsPayOrderEdit')")
  @Operation(summary = "更新")
  @PutMapping
  public R update(@RequestBody FundsPayOrderVo model) {
    return R.run(() -> service.update(model));
  }

  @PreAuthorize("@am.canAccess('fundsPayOrderEdit')")
  @Operation(summary = "编辑更新")
  @PutMapping("edit")
  public R updateForEdit(@RequestBody FundsPayOrderVo model) {
    return R.run(() -> service.updateForEdit(model));
  }

  @PreAuthorize("@am.canAccess('fundsPayOrderDelete')")
  @Operation(summary = "删除")
  @DeleteMapping("{id}")
  public R delete(@PathVariable Long id) {
    return R.run(() -> service.delete(id));
  }

  @PreAuthorize("@am.canAccess('fundsPayOrderDelete')")
  @Operation(summary = "批量删除")
  @DeleteMapping("batch")
  public R batchDelete(@RequestBody IdListPo po) {
    return R.run(() -> service.batchDelete(po.getIds()));
  }

  @Operation(summary = "导出")
  @GetMapping("export")
  @PreAuthorize("@am.canAccess('fundsPayOrderExport')")
  public void export(
    @RequestParam(name = "template", required = false) Long templateId,
    FundsPayOrderSearchParamPo search,
    HttpServletResponse response
  ) throws Exception {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    service.export(templateId, search, response);
  }

  @PreAuthorize("@am.canAccess('fundsPayOrderInvalidate')")
  @Operation(summary = "作废")
  @PostMapping("invalidate")
  public R invalidate(@RequestBody IdPo po) {
    return R.run(() -> service.invalidate(po.getId()));
  }

  @Operation(summary = "全部")
  @GetMapping("all-type")
  public R<List<FundsPayOrderItemVo>> allType(
    FundsPayOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allType(search));
  }

  @Operation(summary = "全部分页")
  @GetMapping("all-typePage")
  public R<Page<FundsPayOrderItemVo>> allTypePage(
    FundsPayOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allTypePage(search));
  }

  @Operation(summary = "全部最后更新时间")
  @GetMapping("all-typeLastUpdateTime")
  public R<Date> allTypeLastUpdateTime(FundsPayOrderSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allTypeLastUpdateTime(search));
  }

  @Operation(summary = "待审批")
  @GetMapping("approval")
  public R<List<FundsPayOrderItemVo>> approval(
    FundsPayOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.approval(search));
  }

  @Operation(summary = "待审批分页")
  @GetMapping("approvalPage")
  public R<Page<FundsPayOrderItemVo>> approvalPage(
    FundsPayOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.approvalPage(search));
  }

  @Operation(summary = "待审批最后更新时间")
  @GetMapping("approvalLastUpdateTime")
  public R<Date> approvalLastUpdateTime(FundsPayOrderSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.approvalLastUpdateTime(search));
  }

  @Operation(summary = "待付款")
  @GetMapping("obligation")
  public R<List<FundsPayOrderItemVo>> obligation(
    FundsPayOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.obligation(search));
  }

  @Operation(summary = "待付款分页")
  @GetMapping("obligationPage")
  public R<Page<FundsPayOrderItemVo>> obligationPage(
    FundsPayOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.obligationPage(search));
  }

  @Operation(summary = "待付款最后更新时间")
  @GetMapping("obligationLastUpdateTime")
  public R<Date> obligationLastUpdateTime(FundsPayOrderSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.obligationLastUpdateTime(search));
  }

  @Operation(summary = "已完成")
  @GetMapping("completed")
  public R<List<FundsPayOrderItemVo>> completed(
    FundsPayOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.completed(search));
  }

  @Operation(summary = "已完成分页")
  @GetMapping("completedPage")
  public R<Page<FundsPayOrderItemVo>> completedPage(
    FundsPayOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.completedPage(search));
  }

  @Operation(summary = "已完成最后更新时间")
  @GetMapping("completedLastUpdateTime")
  public R<Date> completedLastUpdateTime(FundsPayOrderSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.completedLastUpdateTime(search));
  }

  @Operation(summary = "已驳回")
  @GetMapping("reject")
  public R<List<FundsPayOrderItemVo>> reject(
    FundsPayOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.reject(search));
  }

  @Operation(summary = "已驳回分页")
  @GetMapping("rejectPage")
  public R<Page<FundsPayOrderItemVo>> rejectPage(
    FundsPayOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.rejectPage(search));
  }

  @Operation(summary = "已驳回最后更新时间")
  @GetMapping("rejectLastUpdateTime")
  public R<Date> rejectLastUpdateTime(FundsPayOrderSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.rejectLastUpdateTime(search));
  }

  @Operation(summary = "已作废")
  @GetMapping("cancellation")
  public R<List<FundsPayOrderItemVo>> cancellation(
    FundsPayOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.cancellation(search));
  }

  @Operation(summary = "已作废分页")
  @GetMapping("cancellationPage")
  public R<Page<FundsPayOrderItemVo>> cancellationPage(
    FundsPayOrderSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.cancellationPage(search));
  }

  @Operation(summary = "已作废最后更新时间")
  @GetMapping("cancellationLastUpdateTime")
  public R<Date> cancellationLastUpdateTime(FundsPayOrderSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.cancellationLastUpdateTime(search));
  }
}
